package com.dub.firstapp;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.dub.model.SecRpcModel;
import com.dub.utils.Session;
import java.util.List;

public class ShareAdapter extends ArrayAdapter<SecRpcModel.ModelSecRpcAdvert>{
    private int layoutId;
    public ShareAdapter(Context context, int resource, List<SecRpcModel.ModelSecRpcAdvert> objects){
        super(context,resource,objects);
        this.layoutId=resource;
    }
    public View getView(int position, final View convertView, ViewGroup parent) {
        SecRpcModel.ModelSecRpcAdvert item_advert=getItem(position);
        View view= LayoutInflater.from(getContext()).inflate(layoutId,parent,false);
        TextView titleView=(TextView)view.findViewById(R.id.share_item_title);
        titleView.setText(item_advert.getAdTitle());

        String baseUrl=getContext().getString(R.string.cfg_url);
        String url_pre="链接：";
        String price_aft=" 元/次";

        TextView urlView=(TextView)view.findViewById(R.id.share_item_url);
        urlView.setText(url_pre+baseUrl+"/"+ Session.GetSession().GetAgent().getModelSecRpcAgent().getAgentId()+"_"+item_advert.getAdvertId());

        TextView price_View=(TextView)view.findViewById(R.id.share_item_price);
        price_View.setText(item_advert.getAdCSPrice()+price_aft);

        view.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
                TextView txv=(TextView)view.findViewById(R.id.share_item_url);
                String all_url=txv.getText().toString();
                String url = all_url.substring(all_url.indexOf("：")+1);

                ClipboardManager copy=(ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                copy.setPrimaryClip(ClipData.newPlainText("text",url));

                Toast.makeText(getContext(),"复制链接成功",Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }
}